package com.gyms.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.classic.okhttp.beans.HVVenueBean;
import com.classic.okhttp.beans.HVVenueLabelBean;
import com.gyms.R;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import constant.PicSplit;
import glide.ImageLoaderUtils;
import myInterface.OnItemClickLitener;
import utils.DecimalUtils;
import utils.SizeUtils;
import utils.StringUtils;

/**
 * 查找场馆列表Adapter
 * ltx
 */
public class GymsRecyclertAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater mInflater;
    // 场馆图片
    private List<HVVenueBean> venuePic;
    private Context mContext;

    public static final int TYPE_TITLE = 0;//默认布局
    public static final int TYPE_COMPANY = 1;//空布局
    private static final int TYPE_HEAD=2;//头布局

    private View mHeadView;

    public void setHeadView(View mHeadView){
        this.mHeadView=mHeadView;
    }

    private int getHeadCount(){
        return mHeadView==null?0:1;
    }

    public GymsRecyclertAdapter(Context context,List<HVVenueBean> venuePic) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.venuePic = venuePic;
    }

    private OnItemClickLitener mOnItemClickLitener;//Item的点击监听

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemViewType(int position) {//实现多种布局
       if(position==0&&mHeadView!=null){
           return TYPE_HEAD;
       }
       if(StringUtils.isNull(venuePic)){//增加保护
           return -1;
       }
        if(position>venuePic.size()){//增加保护
            return -1;
        }
        HVVenueBean mGymBean=venuePic.get(position-getHeadCount());
        boolean isCanAdd= mGymBean.isCanAdd;
       if(isCanAdd){
            return TYPE_TITLE;
        } else {
            return TYPE_COMPANY;
        }
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view ;
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case TYPE_HEAD:
                holder=new HomeViewHolder(mHeadView);
                break;
            case TYPE_TITLE:
                view=   mInflater.inflate(R.layout.gym_divider_view, parent, false);
                holder=new ViewHolderType2(view);

                break;
            case TYPE_COMPANY:
                view = mInflater.inflate(R.layout.item_recycle_gym, parent, false);
                holder= new GymsRecyclertAdapter.ViewHolder(view);

                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(StringUtils.isNull(venuePic)){
            return;
        }
        if(holder instanceof ViewHolder){
            HVVenueBean bean=venuePic.get(position-getHeadCount());
            if(!StringUtils.isNull(bean)){
                initData((ViewHolder)holder,bean);
            }
            //如果设置了回调，则设置点击事件
            if (mOnItemClickLitener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickLitener.onItemClick(holder.itemView, position-getHeadCount());
                    }
                });
            }
        }else if(holder instanceof ViewHolderType2){
            if(position-getHeadCount()==0){
                ((ViewHolderType2) holder).mTvIsNoHave.setText(R.string.str_no_have);

            }else{
                ((ViewHolderType2) holder).mTvIsNoHave.setText(R.string.str_no_happy);
            }
        }

    }

    private void initData(GymsRecyclertAdapter.ViewHolder mViewHolder,HVVenueBean mHVVenueBean) {
        boolean isCollection=mHVVenueBean.getIsCollection();//收藏
        boolean isRecommend=mHVVenueBean.getIsRecommend();//推荐
        if(!isCollection&&!isRecommend){
            mViewHolder.mIvCollect.setVisibility(View.GONE);
        }else{
            mViewHolder.mIvCollect.setVisibility(View.VISIBLE);
        }
        if (isRecommend) {
            mViewHolder.mIvCollect.setImageResource(R.mipmap.icon_best);
        }
        if (isCollection) {//收藏大于推荐
            mViewHolder.mIvCollect.setImageResource(R.mipmap.icon_save_subscript);
        }
        mViewHolder.mTvGymName.setText(StringUtils.CS(mHVVenueBean.getVenueName()));
        mViewHolder.mTvGymDes.setText(StringUtils.CS(mHVVenueBean.getShortAddress()));
        Double mPrice=null;
        /*if(!StringUtils.isNull(mHVVenueBean.getMinPrice())){
            mPrice = mHVVenueBean.getMinPrice();
        }*/
        /*场馆列表中，非线上预订场馆，没有价格按钮*/
        if (StringUtils.isNull(mPrice)) {
            mViewHolder.mTvPrice.setVisibility(View.GONE);
        } else {
            mViewHolder.mTvPrice.setVisibility(View.VISIBLE);
            mViewHolder.mTvPrice.setText(DecimalUtils.setPriceStyle(mContext,mPrice,R.dimen.price_text_size_14,R.dimen.price_text_size_12,true));
        }
//        mViewHolder.mIvCollect.setVisibility(mHVVenueBean.getIsCollection() ? View.VISIBLE : View.GONE);
        ImageLoaderUtils.display(mContext, mViewHolder.mIvGym, PicSplit.urlAdd(mHVVenueBean.getVenueUrl(),PicSplit.TYPE_GYM_LIST_IMG));
        if (!StringUtils.isNull(mHVVenueBean.getDistance())) {
            double dis = mHVVenueBean.getDistance() / 1000;
            showDis(mViewHolder,dis);
        }else{
            mViewHolder.mTvGymDis.setText("");
        }

        if(!StringUtils.isNull(mHVVenueBean.getVenueDiscount())){
            mViewHolder.mLlGymDiscount.setVisibility(View.GONE);
            String discountUrl=mHVVenueBean.getVenueDiscount().get(0).getDiscountIcon();//优惠图片
            String mDiscountContent=mHVVenueBean.getVenueDiscount().get(0).getDiscountContent();//优惠文本
            if(!StringUtils.isNull(discountUrl)){//优惠的图片为null隐藏
                mViewHolder.mLlGymDiscount.setVisibility(View.VISIBLE);
                mViewHolder.mTvDiscount.setVisibility(View.VISIBLE);
                ImageLoaderUtils.display(mContext, mViewHolder.mTvDiscount, PicSplit.urlAdd(discountUrl,PicSplit.TYPE_TAB));
            }else {
                mViewHolder.mTvDiscount.setVisibility(View.GONE);

            }
            mViewHolder.mTvdistanceText.setText(StringUtils.CS(mDiscountContent));

        }else{//为空就隐藏
            mViewHolder.mLlGymDiscount.setVisibility(View.GONE);
//            mViewHolder.mTvDiscount.setVisibility(View.GONE);
        }

        List<HVVenueLabelBean> mHVVenueLabelBeans = mHVVenueBean.getVenueLabel();
        if(!StringUtils.isNull(mHVVenueLabelBeans)){//标签为null
            mViewHolder.mLLTabAdd.setVisibility(View.VISIBLE);
            mViewHolder.mLLTabAdd.removeAllViews();
            for (int pos = 0; pos < mHVVenueLabelBeans.size(); pos++) {
                //最多显示三个
                if (pos == 3) {//如果第三个就停止循环
                    break;
                }
                ImageView iv = new ImageView(mContext);
                LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, SizeUtils.dp2px(mContext, 15));

                if (pos != 0) {
                    ll.setMargins(SizeUtils.dp2px(mContext, 5), 0, 0, 0);
                }
                iv.setLayoutParams(ll);
                Glide.with(mContext).load(mHVVenueLabelBeans.get(pos).getDiscountIcon()).fitCenter().into(iv);
                mViewHolder.mLLTabAdd.addView(iv);
            }
        }else{
            mViewHolder.mLLTabAdd.setVisibility(View.GONE);
        }

    }
    /**
     * 小于1公里的，距离显示“<1km”；大于等于1公里，小于等于50公里的，距离显示“xx.xkm”；大于50公里的，距离显示“>50km”；
     *
     * @param dis 公里
     */
    private void showDis(GymsRecyclertAdapter.ViewHolder mViewHolder,double dis) {
        String disText;
        if (dis < 1) {
            mViewHolder.mTvGymDis.setText("<1km");
        } else if (dis >= 1 && dis <= 50) {
            disText=DecimalUtils.getDistaceText(dis);
            mViewHolder.mTvGymDis.setText(disText + "km");
        } else {
            mViewHolder.mTvGymDis.setText(">50km");
        }
    }

    @Override
    public int getItemCount() {
        return null == venuePic ? getHeadCount() : venuePic.size()+getHeadCount();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_gym)
        ImageView mIvGym;//场馆图片
        @BindView(R.id.tv_gym_name)
        TextView mTvGymName;//场馆名
        @BindView(R.id.tv_gym_des)
        TextView mTvGymDes;//描述
        @BindView(R.id.tv_gym_dis)
        TextView mTvGymDis;//距离
        @BindView(R.id.ll_tab_add)
        LinearLayout mLLTabAdd; //添加tab的layout
        @BindView(R.id.tv_discount)
        ImageView mTvDiscount;//优惠
        @BindView(R.id.iv_collect)
        ImageView mIvCollect;//是否收藏
        @BindView(R.id.tv_price)
        TextView mTvPrice;
        @BindView(R.id.ll_gym_discount)
        LinearLayout mLlGymDiscount;//优惠信息
        @BindView(R.id.tv_distance_text)
        TextView mTvdistanceText;//优惠文字

        ViewHolder(View view) {
            super(view);
            AutoUtils.autoSize(view);
            ButterKnife.bind(this, view);
        }
    }
    class ViewHolderType2 extends  RecyclerView.ViewHolder{
       public TextView mTvIsNoHave;

        public ViewHolderType2(View itemView) {
            super(itemView);
            mTvIsNoHave= (TextView) itemView.findViewById(R.id.tv_is_no_have);
        }
    }

    private class HomeViewHolder extends RecyclerView.ViewHolder{

        private HomeViewHolder(View itemView) {
            super(itemView);
        }
    }
}
